package com.ambulance.project.biz.service.impl;

import com.ambulance.common.utils.ResultMapUtil;
import com.ambulance.project.biz.dto.admin.ClassResourceDto;
import com.ambulance.project.biz.dto.admin.ResouceAvaibleDto;
import com.ambulance.project.biz.entity.Car;
import com.ambulance.project.biz.entity.Class;
import com.ambulance.project.biz.entity.Driver;
import com.ambulance.project.biz.entity.Patient;
import com.ambulance.project.biz.mapper.*;
import com.ambulance.project.biz.service.CarService;
import com.ambulance.project.biz.service.ClassService;
import com.ambulance.project.biz.service.DriverService;
import com.ambulance.project.biz.service.PatientService;
import com.ambulance.project.biz.dto.admin.ResourceAvailable;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 病员信息表 服务实现类
 * </p>
 *
 * @author zr
 * @since 2020-04-15
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {


    @Autowired
    private CarService carService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private ClassService classService;

    @Autowired
    private MyCustomerMapper dispatchMapper;

    @Autowired
    private PatientService patientService;
    @Override
    @Transactional
    public ResouceAvaibleDto listPatientDto(Integer uid) {
        ResouceAvaibleDto patientDto = new ResouceAvaibleDto();

        Integer carNum = carService.count(new QueryWrapper<Car>().eq("status", 0).eq("uid",uid));
        Integer driverNum = driverService.count(new QueryWrapper<Driver>().eq("status", "0").eq("on_duty_status","1").eq("uid",uid));

        Integer classNum = classService.count(new QueryWrapper<Class>().eq("status", "0").eq("uid",uid).eq("carStatus",0));

        Integer dispatchNum = patientService.count(new QueryWrapper<Patient>().eq("status","0").eq("uid",uid));
        patientDto.setCarNum(carNum);
        patientDto.setClassNum(classNum);
        patientDto.setDriverNum(driverNum);
        patientDto.setDispatchNum(dispatchNum);
        return patientDto;
    }

    @Override
    public ResourceAvailable listResourceDto(Integer uid) {

        ResourceAvailable available = new ResourceAvailable();
        QueryWrapper<Car> carQueryWrapper = new QueryWrapper<>();

        String[] carsParam={"carID","type","vin"};

        carQueryWrapper.select(carsParam).
                eq("status","0").eq("uid",uid);
        List<Car> cars = carService.list(carQueryWrapper);

        String[] driverParam={"driverID", "name", "phone", "sex", "age"};
        QueryWrapper<Driver> driverQueryWrapper = new QueryWrapper<>();
        driverQueryWrapper.select(driverParam).
                eq("status", "0").
                eq("uid",uid).
                eq("on_duty_status", "1");
//        List<Map<String, Object>> drivers = driverService.listMaps(driverQueryWrapper);
        List<Driver> drivers = driverService.list(driverQueryWrapper);

        QueryWrapper<ClassResourceDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("carStatus","0").eq("uid",uid);
        // 可用班组信息
        List<ClassResourceDto> classResourceDtos =
                dispatchMapper.listClassResourceDto(0,0);
        //对象转map
        available.setCars(ResultMapUtil.customerObjFields(cars,carsParam));
        available.setDrivers(ResultMapUtil.customerObjFields(drivers,driverParam));
        available.setClassResources(classResourceDtos);
        return available;
    }
}
